package com.u.social.sale.service.impl;

import com.u.social.sale.entity.ProductStock;
import com.u.social.sale.mapper.ProductStockMapper;
import com.u.social.sale.service.IProductStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alex Wu
 * @since 2025-03-05
 */
@Service
public class ProductStockServiceImpl extends ServiceImpl<ProductStockMapper, ProductStock> implements IProductStockService {


    @Override
    public void decreaseStock(Long productId, Long skuId, Integer num) {
        ProductStock productStock = this.lambdaQuery()
                .eq(ProductStock::getProductId, productId)
                .eq(ProductStock::getSkuId, skuId)
                .one();
        if (productStock == null || productStock.getStock() < num || productStock.getStock() - num < 0) {
            throw new IllegalArgumentException("Insufficient stock to decrease.");
        }
        productStock.setStock(productStock.getStock() - num);
        this.updateById(productStock);
    }
}
